package com.gz.purchase.library;

import android.content.Context;

import com.gz.purchase.library.listener.BaseListener;
import com.gz.purchase.library.util.Constants;
import com.gz.purchase.library.util.CrashHandler;
import com.gz.purchase.library.util.PreferenceModel;
import com.gz.purchase.library.util.SingtonAsyncHttpClient;
import com.gz.purchase.library.util.Tools;
import com.loopj.android.http.RequestParams;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 订单相关的接口
 * author wanner1991
 */
public class IOrder {
    private static final String TAG = IOrder.class.getSimpleName();
    private static  IOrder mInstance;
    private static Lock mLocal;
    private Context mContext;

    static {
        mLocal = new ReentrantLock();
    }

    private IOrder(Context mContext) {
        this.mContext = mContext;
    }

    public static IOrder getInstance(){
        return mInstance;
    }

    public static  void init(Context mContext){
        if (mInstance == null){
            mLocal.lock();
            try {
                if (mInstance == null){
                    mInstance = new IOrder(mContext);
                }
            } finally {
                mLocal.unlock();
            }

        }
    }

    /**
     * 提交订单
     * @param products_contents 商品id + 商品数量+商品价格组成的json字符串
     * @param real_pay 实付
     * @param ssn_note 备注
     * @param listener
     */
    public void submitOrder(String products_contents,double real_pay,double should_pay,String ssn_note,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            params.put("products_contents", products_contents);
            params.put("real_pay",real_pay);
            params.put("should_pay",should_pay);
            params.put("ssn_note",ssn_note);
            CrashHandler.i(TAG, products_contents);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_SUBMIT_ORDER, params, listener);
        }

    }

    /**
     * 获取代购订单列表
     * @param page
     * @param begin_time
     * @param end_time
     * @param order_type
     * @param listener
     */
    public void getPurchaseOrders(int page,String begin_time,String end_time,int order_type,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            params.put("page", page);
            if (begin_time != null){
                params.put("begin_time",begin_time);
            }
            if (end_time != null){
                params.put("end_time",end_time);
            }
            if (order_type > 0){
                params.put("order_type",order_type);
            }
            CrashHandler.i(TAG,params.toString());
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_PURCHASE_ORDERS, params, listener);
            CrashHandler.i(TAG, Constants.URL_GET_PURCHASE_ORDERS+"?"+params.toString()+"88888888888888888888888888888888888");
        }

    }

    /**
     * 删除代购订单
     * @param ssn 订单编号
     * @param listener 回调监听器
     */
    public void deletePurchaseOrder(String ssn,BaseListener listener ){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            params.put("ssn",ssn);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_DELETE_PURCHASE_ORDER, params, listener);
        }

    }


}
